<?php

class Service_Boomkat extends Service implements IService
{
    
    static $moduleName = 'Boomkat Parser';
    static $author = 'KnKill';
    static $version = '0.15';
    static $site = 'boomkat.com';
    static $mask = '.*boomkat.com\/downloads\/.*';
    static $metaFields = array(
        META_LABEL,
        META_GENRES,
        META_RELEASE_DATE,
        META_CATALOG,
        META_REVIEW
    );
    
    private $content;

    public function __construct($url) {
        $this->content = getPage($url);
        phpQuery::newDocumentHTML($this->content);
    }
    
    
    /**
     * getReleaseArtists
     */
    public function getReleaseArtists() {
        $artists = array();
        foreach(pq('#product-header-artist a') as $artist) {
            $artists[] = ucwords(trim(pq($artist)->text()));
        }
        return $artists;
    }
    
    
    /**
     * getReleaseTtitle
     */
    public function getReleaseTitle() {
        $title = trim(pq('#product-header-title')->eq(0)->text());
        return $title;
    }
    
    
    /**
     * getGenres
     */
    public function getGenres() {
        $genres = array();
        foreach(pq('#product-header-genre a') as $genre) {
            $genres[] = ucwords(strtolower(trim(pq($genre)->text())));
        }
        return $genres;
    }
    
    
    /**
     * getLabel
     */
    public function getLabel() {
        $label = trim(pq('#product-header-label a')->eq(0)->text());
        return $label;
    }
    
    
    /**
     * getCatalog
     */
    public function getCatalog() {
        $catalog = trim(pq('#product-header-catalog-number')->eq(0)->text());
        preg_match('/Catalogue Number: (.*)/', $catalog, $catalog);
        return trim($catalog[1]);
    }
    
    
    /**
     * getReleaseDate
     */
    public function getReleaseDate() {
        $released = trim(pq('#product-header-released')->eq(0)->text());
        preg_match('/Released: (.*)/', $released, $released);
        return date('Y-m-00', strtotime(trim($released[1])));
    }


    /**
     * getTracklist
     */
    public function getTracklist() {
        $tracklist = array();
        foreach(pq('.tracks-listing') as $track) {
        
            $pqTrack = pq($track);
        
            $data = trim($pqTrack->find('.track-listing-title')->eq(0)->text());

            preg_match('/([0-9]{1,})\. (.*) : (.*)/mi', $data, $data);
            
            $index = trim($data[1]);
            $artist = ucwords(strtolower(trim($data[2])));
            $title  = trim($data[3]);
            
            $length = trim($pqTrack->find('.track-listing-duration')->eq(0)->text());
            
            $tracklist[(int)$index] = array(
                'artists' => array($artist),
                'title'   => $title,
                'length'  => $length
            );
        }
        return $tracklist;
    }
    
    
    /**
     * getReview
     */
    public function getReview() {
        $review = trim(pq('#product-description-text p')->eq(0)->text());
        return (empty($review)) ? false : $review;
    }

}